package com.cynteka.cyfoman.core.collection;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collection;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 12:45
 */
public class FileLink extends BaseLink<File, File> {
    public boolean isFilterHiddenFiles() {
        return filterHiddenFiles;
    }

    public void setFilterHiddenFiles(boolean filterHiddenFiles) {
        this.filterHiddenFiles = filterHiddenFiles;
    }

    private boolean filterHiddenFiles = false;

    public FileLink(String id, String fileCollectionId) {
        super(id, fileCollectionId, fileCollectionId, LinkType.ONE_TO_MANY);
    }

    @Override
    public Collection<File> getDestinationItems(File file) {
        FileFilter filter = new HiddenFileFilter(filterHiddenFiles);
        File[] files = file.listFiles(filter);
        return Arrays.asList(files == null ? new File[0] : files);
    }

    @Override
    public Collection<File> getSourceItems(File file) {
        File[] res;
        ICollection<?> collection = getOwner().getCollection(getCollectionSourceId());
        if (collection instanceof FileCollection) {
            FileCollection fileCollection = (FileCollection) collection;
            File baseFolder = fileCollection.getBaseFolder();
            File parentFile = file.getParentFile();
            if (baseFolder.equals(parentFile)) {
                return Arrays.asList(new File[]{});
            }
        }
        return file.getParentFile() != null ? Arrays.asList(new File[]{file.getParentFile()}) : Arrays.asList(new File[]{});
    }
}
